package top.likeqc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.likeqc.dto.CourseDTO;
import top.likeqc.entity.*;
import top.likeqc.mapper.CourseMapper;
import top.likeqc.service.*;
import top.likeqc.util.SecurityUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程表 服务实现类
 * </p>
 *
 * @author likeqc
 * @since 2022-04-10
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    ITeacherService teacherService;

    @Autowired
    ICoursePointService coursePointService;

    @Autowired
    IPointService pointService;

    @Autowired
    ITargetService targetService;

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SecurityUtil securityUtil;

    @Override
    public Page list(Page page, String key) {

        SysUser sysUser = securityUtil.getSysUser();
        Page<Course> data = null;
        if (sysUser.hasRoleAdmin()) {
            data = page(page, new QueryWrapper<Course>().like(StringUtils.isNotBlank(key), "name", key));
        } else if (sysUser.hasRoleTeacher()) {

            Teacher teacher = teacherService.getOne(new QueryWrapper<Teacher>().eq("user_id", sysUser.getId()));
            data = page(page, new QueryWrapper<Course>().like(StringUtils.isNotBlank(key), "name", key).eq(
                "teacher_id", teacher.getId()));
        }

        if (null != data) {
            data.getRecords().forEach(course -> {
                // 获取教师信息
                course.setTeacher(teacherService.getById(course.getTeacherId()));
                // 获取指标点信息(课程-指标点 多对多关系)
                List<CoursePoint> coursePointList = coursePointService.list(new QueryWrapper<CoursePoint>().eq("course_id",
                    course.getId()));

                List<Long> pointIds = coursePointList.stream().map(map -> map.getPointId()).collect(Collectors.toList());
                List<Point> points = new ArrayList<>();
                if (pointIds.size() > 0) {
                    points = pointService.listByIds(pointIds);
                }
                course.setPointList(points);

                // 获取课程目标信息
                List<Target> targetList = targetService.list(new QueryWrapper<Target>().eq("course_id", course.getId()));
                course.setTargetList(targetList);

            });
        }

        return data;
    }

    @Override
    public boolean edit(CourseDTO courseDTO) {
        Course course = courseDTO2Course(courseDTO);
        boolean b = saveOrUpdate(course);

        // 课程支撑数据
        Long courseId = course.getId();
        List<Long> pointIds = courseDTO.getPointIds();
        if (courseId != null && null != pointIds && pointIds.size() > 0) {
            coursePointService.remove(new QueryWrapper<CoursePoint>().eq("course_id", courseId));
            List<CoursePoint> coursePointList = new ArrayList<>();
            pointIds.forEach(pointId -> {
                CoursePoint coursePoint = new CoursePoint();
                coursePoint.setCourseId(courseId);
                coursePoint.setPointId(pointId);
                coursePointList.add(coursePoint);
            });
            coursePointService.saveBatch(coursePointList);
        }

        return b;
    }

    private Course courseDTO2Course(CourseDTO courseDTO) {
        Course course = new Course();
        course.setId(courseDTO.getId());
        course.setCode(courseDTO.getCode());
        course.setName(courseDTO.getName());
        course.setInfo(courseDTO.getInfo());
        course.setCover(courseDTO.getCover());
        course.setTeacherId(courseDTO.getTeacherId());
        course.setCredit(courseDTO.getCredit());
        course.setHour(courseDTO.getHour());
        course.setType(courseDTO.getType());
        course.setMajor(courseDTO.getMajor());
        course.setEndRatio(courseDTO.getEndRatio());

        return course;
    }

}
